/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.view;

import static com.googlecode.duplicatedetector.i18n.Messages._;

import java.io.IOException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

import com.googlecode.duplicatedetector.controller.ExitActionListener;
import com.googlecode.duplicatedetector.controller.OpenHyperlink;
import com.googlecode.duplicatedetector.i18n.Keys;

/**
 * General panel used to show help and license contents.
 * 
 * @author Marco Biscaro
 */
public class HelpPanel extends JPanel implements Keys {

	private static final long serialVersionUID = 217328060070239324L;
	private URL url;
	private JEditorPane helpEditorPane;
	private JButton okButton;

	public HelpPanel(URL url) {
		this.url = url;
		initialize();
	}

	private void initialize() {
		setLayout(new MigLayout("wrap 1", "[grow][]", "[grow]"));
		add(new JScrollPane(getEditorTextPane()), "w :650:, h :400:");
		add(getOkButton(), "w :100:, align right");
	}

	private JEditorPane getEditorTextPane() {
		if (helpEditorPane == null) {
			try {
				helpEditorPane = new JEditorPane(url);
				helpEditorPane.setEditable(false);
				helpEditorPane.addHyperlinkListener(new OpenHyperlink());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return helpEditorPane;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("    " + _(OK) + "    ");
			okButton.addActionListener(new ExitActionListener(this));
		}
		return okButton;
	}

}
